﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmartMD.Manager.Common;
using SmartMD.DataModel.Entity;

namespace SmartMD.Manager.SysInfo
{
    /// <summary>
    /// Interaction logic for NewExamItemWnd.xaml
    /// </summary>
    public partial class NewExamItemDialog : Window, IVerifiableControl
    {
        private ExamItemMaintain owner = null;

        public object Item
        {
            get { return this.DataContext; }
            set { this.DataContext = value; }
        }
        public UIElement Control
        {
            get { return this; }
        }

        public NewExamItemDialog(Object owner)
        {
            this.owner = (ExamItemMaintain)owner;
            InitializeComponent();
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {         
            string name = this.nameTxt.Text;
            string desc = this.descTxt.Text;
            ExamItemInfo eItem = new ExamItemInfo();
            eItem.Id = System.Guid.NewGuid().ToString();        
            eItem.LastModify = System.DateTime.Now;
            eItem.State = DataModel.RecordState.Added;
            eItem.IsNew = true;
            eItem.Name = name;
            eItem.Description = desc;
            this.owner.DataSource = eItem;
            this.Close();
        }

        public VerifyResult Verify()
        {

            string id = this.nameTxt.Text;
            if (String.IsNullOrWhiteSpace(id))
            {
                return new VerifyResult(false, "标识不能为空");
            }
            if (id.Length > 16 || id.Length < 4)
            {
                return new VerifyResult(false, "标识长度为4到16位");
            }
            char firstChar = id[0];
            if (!Char.IsLetter(firstChar))
            {
                return new VerifyResult(false, "标识应以字母开头");
            }
           /* if (SingletonManager.Online)
            {
                var d = DataServiceManager.DatasetService.Get(id);
                if (d != null)
                {
                    return new VerifyResult(false, "已存在该标识的数据集，请重新填写标识");
                }
            }*/
            return new VerifyResult(true, null);
        }
    }
}
